<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:key name="nodes" match="node" use="@id"/>
               <xsl:template match="/">

                   <fo:root>
                       
                      <fo:layout-master-set>
                <fo:simple-page-master master-name="pagetitle"
                    page-width="210mm" page-height="297mm"
                    margin-top="1cm"   margin-bottom="1cm"
                    margin-left="2cm"  margin-right="2cm">
                    <fo:region-body margin-top="8cm" margin-bottom="8cm" margin-left="4cm" margin-right="4cm"/>
                    <fo:region-before extent="1cm"/>
                    <fo:region-after  extent="1cm"/>
                    <fo:region-start  extent="1.5cm"/>
                    <fo:region-end    extent="1.5cm"/>
                </fo:simple-page-master>   
                          
                          <fo:simple-page-master master-name="toc"
                              page-width="210mm" page-height="297mm"
                              margin-top="1cm"   margin-bottom="1cm"
                              margin-left="2cm"  margin-right="2cm">
                              <fo:region-body margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm"/>
                              <fo:region-before extent="1cm"/>
                              <fo:region-after  extent="1cm"/>
                              <fo:region-start  extent="2cm"/>
                              <fo:region-end    extent="2cm"/>
                          </fo:simple-page-master> 
                          
                    <fo:simple-page-master master-name="allpages"
                              page-width="210mm" page-height="297mm"
                              margin-top="3cm"   margin-bottom="3cm"
                              margin-left="2cm"  margin-right="2cm">
                              <fo:region-body margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm"/>
                              <fo:region-before extent="1cm"/>
                              <fo:region-after  extent="1cm"/>
                              <fo:region-start  extent="2cm"/>
                              <fo:region-end    extent="2cm"/>
                          </fo:simple-page-master> 
                          

                      </fo:layout-master-set>
                       
                       <fo:page-sequence master-reference="pagetitle">
                           <fo:flow flow-name="xsl-region-body">
                               <fo:block  font-size="23pt"
                                   font-family="sans-serif"
                                   font-weight="bold"
                                   text-align="center"
                                   >
                                   Significant Properties
                                   Taxonomy 
                               </fo:block>
                           </fo:flow>
                       </fo:page-sequence>
                       
                       <fo:page-sequence master-reference="toc">
                           <fo:flow flow-name="xsl-region-body">
                               <fo:block  font-size="20"
                                   font-family="sans-serif"
                                   font-weight="bold"
                                   text-align="left"
                                   id="TOC."
                                   >
                                   Table of Contents
                               </fo:block >
                               <xsl:apply-templates  mode="toc"/>
                           </fo:flow>
                       </fo:page-sequence>
                       
                       <fo:page-sequence master-reference="allpages">
                           <fo:flow flow-name="xsl-region-body">
                                     <xsl:apply-templates  mode="node"/>
                           </fo:flow>
                       </fo:page-sequence>
                   </fo:root>
               </xsl:template> 
    
    
    
    <xsl:template match="node" mode="toc">
        <xsl:choose>
        <xsl:when test="@id">
            <fo:block font-size="smaller"
                            space-after=".1in" 
                            space-before=".15in" 
                            text-align-last="justify">
                <fo:basic-link  internal-destination="{@id}">
                    <xsl:value-of select="@id"/> -  
                    <xsl:value-of select="@label"/>
                    <fo:leader leader-pattern="dots"/> 
                    <fo:page-number-citation ref-id="{@id}"/>
                </fo:basic-link> 
            </fo:block>
        </xsl:when>
        <xsl:otherwise>
            <fo:block font-size="smaller"
                space-after=".1in" 
                space-before=".15in" 
                text-align-last="justify">
                <fo:basic-link  internal-destination="{generate-id()}"> 
                    <xsl:value-of select="@label"/>
                    <fo:leader leader-pattern="dots"/>
                    <fo:page-number-citation ref-id="{generate-id()}"/>
                </fo:basic-link>
            </fo:block>
        </xsl:otherwise>
        </xsl:choose>       
        <xsl:apply-templates mode="toc" select="isComposedBy"/>
    </xsl:template>
    
    
    
    
    
    <xsl:template match="node" mode ="node">
        <xsl:choose>
            <xsl:when test="@id">
       <fo:block  font-size="14pt"
                                   font-family="sans-serif"
                                   font-weight="bold"
                                   text-align="center"
                                   id = "{attribute::id}"
                                   space-after="30pt"
                                   space-before="20pt"
                                   >
           <xsl:value-of select="@id"/> -  <xsl:value-of select="@label"/> 
       </fo:block>
            </xsl:when>
            <xsl:otherwise>
                <fo:block  font-size="14pt"
                    font-family="sans-serif"
                    font-weight="bold"
                    text-align="center"
                    id = "{generate-id()}"
                    space-after="20pt"
                    space-before="20pt"
                    >
                    <xsl:value-of select="@label"/> 
                </fo:block>
            </xsl:otherwise>
           </xsl:choose>
        <fo:block  font-size="12pt"
                                   font-family="sans-serif"
                                   font-weight="normal"
                                   text-align="left"
                                   space-after="15pt"
                                   space-before="10pt"
                                   >
            Path: 
            <xsl:for-each select="ancestor-or-self::node">
                <xsl:value-of select="@label"/>
                <xsl:text>/</xsl:text>
            </xsl:for-each>
        </fo:block>
        <xsl:if test="hasNote">
            <fo:block  font-size="12pt"
                font-family="sans-serif"
                font-weight="bold"
                text-align="left"
                space-before="15pt"
                >
                Note:
            </fo:block>
                   <fo:block  font-size="12pt"
                font-family="sans-serif"
                font-weight="normal"
                text-align="left"
                space-after="15pt"
                >
            (type: <xsl:value-of select="hasNote/@type"/>)
                <xsl:value-of select="hasNote"/>              
           </fo:block>
        </xsl:if>
        <xsl:if test="isComposedBy">
            <fo:block  font-size="12pt"
                font-family="sans-serif"
                font-weight="bold"
                text-align="left"
                space-after="15pt"
                >
            Is composed by: 
                </fo:block>
        </xsl:if>
        <fo:block  font-size="12pt"
            font-family="sans-serif"
            font-weight="normal"
            text-align="left"
            space-after="10pt"
            >
            <xsl:for-each select="isComposedBy/node">
    
                    <xsl:choose>
                        <xsl:when test ="@id"> <fo:basic-link internal-destination="{@id}"><xsl:value-of select="@label"/></fo:basic-link></xsl:when>
                        <xsl:otherwise> <fo:basic-link internal-destination="{generate-id()}"><xsl:value-of select="@label"/></fo:basic-link></xsl:otherwise>
                    </xsl:choose>
                          
            </xsl:for-each>
        </fo:block>
        
        <xsl:if test="isRelatedTo">
            <fo:block  font-size="12pt"
                font-family="sans-serif"
                font-weight="bold"
                text-align="left"
                space-after="15pt"
                >
            Is related to: 
            </fo:block>
            <fo:block  font-size="12pt"
                font-family="sans-serif"
                font-weight="normal"
                text-align="left"
                >
                            <xsl:for-each select="isRelatedTo/node">
                    <fo:basic-link internal-destination="{@id}"> <xsl:value-of select="key('nodes',@id)/@label"/> </fo:basic-link>
                </xsl:for-each>
        </fo:block>    
	</xsl:if>
            <fo:block  font-size="10pt"
                font-family="sans-serif"
                font-weight="bold"
                text-align="left"
                space-after="5pt"
                >
                <xsl:if test="ancestor::node">
                [<fo:basic-link internal-destination="{ancestor::node[1]/@id}">Back to parent]</fo:basic-link>
                </xsl:if>
                [<fo:basic-link internal-destination="TOC.">TOC]</fo:basic-link>
	</fo:block> 
            
        <xsl:apply-templates select="isComposedBy" mode="node"/>
    </xsl:template>

    
</xsl:stylesheet>
